package com.rykj.util;


import com.lcw.library.imagepicker.compressimage.CompressImageInfoGetter;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;

import androidx.annotation.Keep;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;


public class Token2UrlFunc implements Func1<Token2UrlFunc.OSSPlainTextAKSKCredentialInfo, Observable<LinkedHashMap<String, String>>> {

    public class OSSPlainTextAKSKCredentialInfo {
        public String accessKeyId;
        public String accessKeySecret;
        public String securityToken;
    }

    //与个人的存储区域有关
    private static final String ENDPOINT = "http://oss-cn-beijing.aliyuncs.com";
    //上传仓库名
    private static final String BUCKET_NAME = "ruanyun-xuni";
    //阿里云API的文件夹名称
    private static final String FOLDER = "xuni/";

//    private static OSS getOSSClient(OSSPlainTextAKSKCredentialInfo info) {
//        OSSStsTokenCredentialProvider stsTokenCredentialProvider = new OSSStsTokenCredentialProvider(
//                info.accessKeyId,
//                info.accessKeySecret,
//                info.securityToken);
////        OSSCredentialProvider credentialProvider =
////                new OSSPlainTextAKSKCredentialProvider("LTAIl66NBLpI6cWg",
////                        "dsT5Z8bSYDsUEk8I3g8G7h1IRFIkiy");
//        return new OSSClient(App.getInstance(), ENDPOINT, stsTokenCredentialProvider);
//    }

    private List<InputInfo> mInputInfos;
    private static String userNum;

    public Token2UrlFunc(String userNum, List<InputInfo> inputInfos) {
        mInputInfos = inputInfos;
        Token2UrlFunc.userNum = userNum;
    }

    /**
     * 上传方法
     * @param objectKey
     *         标识
     * @param path
     *         需上传文件的路径
     * @return 外网访问的路径
     */
    private static String upload(OSSPlainTextAKSKCredentialInfo info, String objectKey, String path) {
        // 构造上传请求
//        PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, FOLDER + objectKey, path);
//        try {
//            OSS client = getOSSClient(info);
//            PutObjectResult result = client.putObject(request);
//            if(result.getStatusCode() == 200){
//                return objectKey;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return null;
    }


    @Override
    public Observable<LinkedHashMap<String, String>> call(OSSPlainTextAKSKCredentialInfo info) {

        return Observable.unsafeCreate(new Token2UpSubscribe(mInputInfos, info));
    }


    static class Token2UpSubscribe implements Observable.OnSubscribe<LinkedHashMap<String, String>> {

        List<InputInfo> mInputInfos;
        OSSPlainTextAKSKCredentialInfo token;

        Token2UpSubscribe(List<InputInfo> inputInfos, OSSPlainTextAKSKCredentialInfo token) {
            mInputInfos = inputInfos;
            this.token = token;
        }

        @Override
        public void call(Subscriber<? super LinkedHashMap<String, String>> subscriber) {
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            for (int i = 0; i < mInputInfos.size(); i++) {
                InputInfo inputInfo = mInputInfos.get(i);
                File idCardFront = inputInfo.upFile;
                String upName;
                if (idCardFront != null) {
                    String fileName = idCardFront.getName();
                    String suffix = fileName.substring(fileName.lastIndexOf("."));

                    String name = userNum + "_" + inputInfo.key + i + "_" + System.currentTimeMillis();
                    String objectKey = MD5.md5(name) + suffix;
                    upName = upload(token, objectKey, idCardFront.getPath());
                } else {
                    upName = null;
                }
                map.put(inputInfo.key, upName);
            }
            subscriber.onNext(map);
        }
    }


    @Keep
    public static class InputInfo implements CompressImageInfoGetter {
        String key;
        File upFile;

        public InputInfo(String key, File upFile) {
            this.key = key;
            this.upFile = upFile;
        }

        @Override
        public void setParamsName(String paramsName) {
            key = paramsName;
        }

        @Override
        public String imageFilePath() {
            return upFile.getPath();
        }

        @Override
        public String imageFileName() {
            return upFile.getName();
        }

        @Override
        public String requestPramsName() {
            return key;
        }
    }


}
    
 